<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class TypeController extends Controller
{
    public function getIndex()
    {
        $data=DB::table('type')->get();
        return view('admin.type.index',['data'=>$data]);
    }

    public function getAdd()
    {
        return view('admin.type.add');
    }

    public function postInsert(Request $request)
    {
        $data=$request->except('_token');
        $z=DB::table('type')->insert($data);
        if($z){
            return redirect('admin/type/index')->with('success','添加成功');
        }else{

            return back()->with('error','添加失败');
        }
    }

    public function getEdit($id)
    {
        $data=DB::table('type')->where('type_id',$id)->first();
        return view('admin.type.edit',['data'=>$data]);
    }

    public function postUpdata(Request $request,$id)
    {
        $data=$request->except('_token');
        $z= DB::table('type')->where('type_id',$id)->update($data);
        if($z){
            return redirect('admin/type/index')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }

    public function getDelete($id)
    {
        $z=DB::table('type')->where('type_id',$id)->delete();
        if($z){

            $data=[
                'status'=>0,
            ];
        }else{
            $data=[
                'status'=>1,
            ];

        }
        return $data;
    }
}
